package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.pojo.User;
import net.zjitc.service.CategoryService;
import net.zjitc.service.UserService;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.service.impl.UserServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/11/29.
 */
public class UserController {
    UserService userService=new UserServiceImpl();
    //商城用户
        //跳转到登录页面
    @URLMapping(url = "/loginUI")
    public String loginUI(HttpServletRequest request, HttpServletResponse response){
        //菜单栏显示一级菜单
        CategoryService service=new CategoryServiceImpl();
        List<Category> clist=service.findAll();
        request.setAttribute("clist",clist);
        return "/WEB-INF/user/login.jsp";
    }
    //跳转到注册页面
    @URLMapping(url = "/registUI")
    public String registUI(HttpServletRequest request, HttpServletResponse response){
        //菜单栏显示一级菜单
        CategoryService service=new CategoryServiceImpl();
        List<Category> clist=service.findAll();
        request.setAttribute("clist",clist);
        return "/WEB-INF/user/regist.jsp";
    }

    //ajax  查找用户名
    @URLMapping(url = "/findUsernameServlet")
    public String findUsernameServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username=request.getParameter("username");
        User user=userService.findUsername(username);
        if (user!=null){
            response.getWriter().print(true);
        }else {
            response.getWriter().print(false);
        }
        return null;
    }

    //注册
    @URLMapping(url = "/regist")
    public String regist(HttpServletRequest request, HttpServletResponse response){
        //输入的验证码
        String inputvail=request.getParameter("inputvail");
        //正确的验证码
        String truevail= (String) request.getSession().getAttribute("vaildateCode");
        //equalsIgnoreCase:忽视大小写
        if (!inputvail.equalsIgnoreCase(truevail)){
            request.setAttribute("codeMsg","验证码有误");
            return "/WEB-INF/user/regist.jsp";
        }
        //用户名错误
        String username=request.getParameter("username");
        boolean flagusername=username.matches("^[a-zA-Z]\\w{2,11}$");
        if (flagusername!=true){
            request.setAttribute("msg","用户名格式错误");
            return "/WEB-INF/user/regist.jsp";
        }
        //密码格式错误
        String password=request.getParameter("password");
        boolean flagpassword=password.matches("^[a-zA-Z0-9]{6,12}$");
        if (flagpassword!=true){
            request.setAttribute("passwordMsg","密码格式错误");
            return "/WEB-INF/user/regist.jsp";
        }
        //电话格式错误
        String phone=request.getParameter("phone");
        boolean flagphone=phone.matches("^1[3578]\\d{9}$");
        if (flagphone!=true){
            request.setAttribute("phoneMsg","电话格式错误");
            return "/WEB-INF/user/regist.jsp";
        }
        //邮箱格式错误
        String email=request.getParameter("email");
        boolean flagemail=email.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
        if (flagemail!=true){
            request.setAttribute("emailMsg","邮箱格式错误");
            return "/WEB-INF/user/regist.jsp";
        }

        //接收用户信息
        User user=new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
            int count=userService.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/WEB-INF/user/regismsg.jsp";
    }

    //激活状态码
    @URLMapping(url = "/activeCode")
    public String activeCode(HttpServletRequest request, HttpServletResponse response){
        String code=request.getParameter("code");
        //通过code（用到uuid）查询用户
        User user=userService.findByCode(code);
        if (user!=null){
            //修改用户状态
            user.setCode(null);
            user.setState(1);
            //更新用户
            int count=userService.update(user);
            request.setAttribute("acmsg","激活成功，请登录");
            //跳转到激活成功页面
            return "/WEB-INF/user/activeCode.jsp";
        }else {
            request.setAttribute("acmsg","激活失败，请登录");
            //跳转到激活成功页面
            return "/WEB-INF/user/msg.jsp";
        }
    }
    //用户登录
    @URLMapping(url = "/user_login")
    public String user_login(HttpServletRequest request, HttpServletResponse response){
        //输入的验证码
        String inputvail=request.getParameter("inputvail");
        //正确的验证码
        String truevail= (String) request.getSession().getAttribute("vaildateCode");
        //equalsIgnoreCase:忽视大小写
        if (!inputvail.equalsIgnoreCase(truevail)){
            request.setAttribute("codeMsg","验证码有误");
            //菜单栏显示一级菜单
            CategoryService service=new CategoryServiceImpl();
            List<Category> clist=service.findAll();
            request.setAttribute("clist",clist);
            return "/WEB-INF/user/login.jsp";
        }
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        User user=userService.findByCondition(username,password);
        HttpSession session=request.getSession();
        if (user==null || (user.getState()==0)){
            request.setAttribute("lmsg","用户名或密码错误,或者还未激活");
            return "/WEB-INF/user/login.jsp";
        }else {
            //登录成功
            session.setAttribute("session_user",user);
            return "Redirect:"+request.getContextPath()+"/index";
        }

    }
    //退出登录
    @URLMapping(url = "/user_exit")
    public String user_exit(HttpServletRequest request, HttpServletResponse response){
        HttpSession session=request.getSession();
        session.removeAttribute("session_user");
        return "Redirect:"+request.getContextPath()+"/index";
    }
    //跳转到首页
    @URLMapping(url = "/shopIndex")
    public String shopIndex(HttpServletRequest request, HttpServletResponse response){

        return "Redirect:"+request.getContextPath()+"/index";
    }




}
